import { notification, message } from 'antd';
import cloneDeep from 'lodash/cloneDeep';
import _map from 'lodash/map';
import _uniq from 'lodash/uniq';
import _pull from 'lodash/pull';
import { GetDateThree, FormatnumberZero } from '../../../function';
import { getFssFundOptionsListByStock } from '../../../services/allposition_service';
import {
  upLoadPositionStocks,
  getLastDayPosition,
  getPositionSourceList,
  deletePositionSource,
  updatePositionSource,
  addPositionSource,
  slitPositionStocks,
  getUBSSellStock,
  updateStockSellPrice,
  upLoadPositionOptions,
  getLastDayOption,
  slitPositionOptions,
  getSellList,
  updateOptionSellPrice,
  getLastDayOptionByCode,
  upLoadStocks,
  upLoadBOND,
  startRunPoint,
} from '../../../services/rui_service';

export default {
  namespace: 'positionrui',
  state: {
    options: { list: [], code: 0, message: '' },
    optionsFilters: {},
    optionsByCode: { list: [] },
    optionsByCodeFilters: {},
    optionsell: { list: [] },
    optionsellFilters: {},
    optionsmodal: { list: [] },
    optionsmodalFilters: {},
    stocks: { list: [], code: 0, message: '' },
    stocksFilters: {},
    stocksSource: { list: [], pagination: { total: 0, pageSize: 10, current: 1 } },
    stocksell: { list: [] },
    stocksellFilters: {},
  },

  effects: {
    // stock
    *UpLoadPositionStocks({ payload }, { call, put }) {
      const response = yield call(upLoadPositionStocks, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *GetLastDayPositionStocks({ payload, callback }, { call, put }) {
      const response = yield call(getLastDayPosition, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        // message.success(response.message)
        const industry = _pull(_uniq(_map(response.list, 'industry')), undefined);
        const sourceperson = _pull(_uniq(_map(response.list, 'sourceperson')), undefined);
        yield put({
          type: 'saveStocks',
          payload: {
            list: response.list,
            code: response.code,
            message: response.message,
            filter: { industry, sourceperson },
          },
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *GetPositionSourceList({ payload, callback }, { call, put }) {
      const response = yield call(getPositionSourceList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        // message.success(response.message)
        yield put({
          type: 'saveStocksSource',
          payload: { list: response.list, pagination: response.pagination },
        });
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *DeletePositionSource({ payload, callback }, { call, put }) {
      const response = yield call(deletePositionSource, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *UpdatePositionSource({ payload, callback }, { call, put }) {
      const response = yield call(updatePositionSource, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *AddPositionSource({ payload, callback }, { call, put }) {
      const response = yield call(addPositionSource, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *SlitPositionStocks({ payload, callback }, { call, put }) {
      const response = yield call(slitPositionStocks, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // sell
    *GetUBSSellStock({ payload, callback }, { call, put }) {
      const response = yield call(getUBSSellStock, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        const type = _pull(_uniq(_map(response.data, 'type')), undefined);
        const buydate = _pull(_uniq(_map(response.data, 'buydate')), null, undefined);
        const selldate = _pull(_uniq(_map(response.data, 'selldate')), null, undefined);
        yield put({
          type: 'saveStocksSells',
          payload: { data: { list: response.list }, filter: { selldate, buydate, type } },
        });
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *UpdateStockSellPrice({ payload, callback }, { call, put }) {
      const response = yield call(updateStockSellPrice, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });

        if (callback) callback(false);
      }
    },

    // option
    *UpLoadPositionOptions({ payload }, { call, put }) {
      const response = yield call(upLoadPositionOptions, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *GetLastDayOption({ payload, callback }, { call, put }) {
      const response = yield call(getLastDayOption, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        // message.success(response.message)
        const sourceperson = _pull(_uniq(_map(response.data, 'sourceperson')), undefined);
        const attribute = _pull(_uniq(_map(response.data, 'attribute')), null, undefined);
        const type = _pull(_uniq(_map(response.data, 'type')), null, undefined);
        yield put({
          type: 'saveOptions',
          payload: {
            data: { list: response.data, code: response.code, message: response.message },
            filter: { attribute, sourceperson, type },
          },
        });
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *SlitPositionOptions({ payload, callback }, { call, put }) {
      const response = yield call(slitPositionOptions, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // sell
    *GetSellList({ payload, callback }, { call, put }) {
      const response = yield call(getSellList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        // let publisher = _pull(_uniq(_map(response.data,'publisher')),undefined);
        // let attribute = _pull(_uniq(_map(response.data,'attribute')),null,undefined);
        const type = _pull(_uniq(_map(response.data, 'type')), null, undefined);
        const adddate = _pull(_uniq(_map(response.data, 'adddate')), null, undefined);
        yield put({
          type: 'saveOptionsSells',
          payload: { data: { list: response.data }, filter: { type, adddate } },
        });
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    *UpdateOptionSellPrice({ payload, callback }, { call, put }) {
      const response = yield call(updateOptionSellPrice, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
        if (callback) callback(false);
      }
    },

    *GetLastDayOptionByCode({ payload, callback }, { call, put }) {
      const response = yield call(getLastDayOptionByCode, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        // message.success(response.message)
        const sourceperson = _pull(_uniq(_map(response.data, 'sourceperson')), undefined);
        const attribute = _pull(_uniq(_map(response.data, 'attribute')), null, undefined);
        const type = _pull(_uniq(_map(response.data, 'type')), null, undefined);
        yield put({
          type: 'saveOptionsByCode',
          payload: { data: { list: response.data }, filter: { attribute, sourceperson, type } },
        });
        if (callback) callback();
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // optionmodal
    *GetFssFundOptionsList({ payload, callback }, { call, put }) {
      const response = yield call(getFssFundOptionsListByStock, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveOptionsModal',
        payload: {
          list: {
            list: response.rows,
            pagination: { current: 1, pageSize: 30, total: response.rows.length - 1 },
          },
          filter: {},
        },
      });
      if (callback) callback(response);
    },
    *ChangeOptionsPage({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeOptionsModalPage',
        payload: { current: payload.page, pageSize: payload.num, total: payload.total },
      });
      if (callback) callback();
    },

    // optionmodal
    *GetFssFundOptionsListClear({ payload, callback }, { call, put }) {
      yield put({
        type: 'clearOptionsModal',
      });
      if (callback) callback();
    },

    // 上传股票
    *UpLoadStocks({ payload }, { call, put }) {
      const response = yield call(upLoadStocks, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // 上传债券
    *UpLoadBOND({ payload }, { call, put }) {
      const response = yield call(upLoadBOND, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    *RunPoints({ payload }, { call, put }) {
      const response = yield call(startRunPoint, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        message.success(response.message);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
  },

  reducers: {
    saveOptions(state, action) {
      return {
        ...state,
        options: action.payload.data,
        optionsFilters: action.payload.filter,
      };
    },

    saveOptionsByCode(state, action) {
      return {
        ...state,
        optionsByCode: action.payload.data,
        optionsByCodeFilters: action.payload.filter,
      };
    },

    saveOptionsSells(state, action) {
      return {
        ...state,
        optionsell: action.payload.data,
        optionsellFilters: action.payload.filter,
      };
    },

    saveOptionsModal(state, action) {
      return {
        ...state,
        optionsmodal: action.payload.list,
        optionsmodalFilters: action.payload.filter,
      };
    },

    clearOptionsModal(state, action) {
      return {
        ...state,
        optionsmodal: { list: [] },
        optionsmodalFilters: {},
      };
    },

    changeOptionsModalPage(state, action) {
      return {
        ...state,
        optionsmodal: {
          ...state.optionsmodal,
          pagination: {
            total: action.payload.total,
            current: action.payload.current,
            pageSize: action.payload.pageSize,
          },
        },
      };
    },

    saveStocks(state, action) {
      return {
        ...state,
        stocks: action.payload,
        stocksFilters: action.payload.filter,
      };
    },

    saveStocksSource(state, action) {
      return {
        ...state,
        stocksSource: action.payload,
      };
    },

    saveStocksSells(state, action) {
      return {
        ...state,
        stocksell: action.payload.data,
        stocksellFilters: action.payload.filter,
      };
    },
  },
};
